package com.sgb.kjwl.viewmodel.home;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.sgb.kjwl.model.entity.home.EnterpriseRecPackEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class CorporateWebsiteModel extends HBaseViewModel {

    public MutableLiveData<List<EnterpriseEditEntity>> menuList = new MutableLiveData<>();

    public MutableLiveData<List<EnterpriseRecEntity>> companyList = new MutableLiveData<>();

    public CorporateWebsiteModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 根据用户身份信息获取展示的企业标题
     */
    public void getCompanyTitleList() {
        APIManager.getInstance().getBigHomeAPI().getEnterpriseMenuList().enqueue(new ICallBack<BaseEntity<List<EnterpriseEditEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseEditEntity>> data) {
                menuList.postValue(data.getData());
            }
        });
    }

    /**
     * 根据类型获取首页企业列表(分页)
     */
    public void getCompanyListByType(String key, int page) {
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        Map map = new HashMap();
        map.put("page", page);
        map.put("limit", 50);
        map.put("areaCode", areaCode);
        map.put("type", key);
        APIManager.getInstance().getBigHomeAPI().getCompanyListByType(map).enqueue(new ICallBack<BaseEntity<EnterpriseRecPackEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<EnterpriseRecPackEntity> data) {
                if (data != null && data.getData() != null && data.getData().getRecords() != null) {
                    companyList.postValue(data.getData().getRecords());
                } else {
                    companyList.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<EnterpriseRecPackEntity>> call, Throwable t) {
                super.onFailure(call, t);
                companyList.postValue(new ArrayList<>());
            }
        });
    }
}
